package org.jzs.mybaseapp.section.home.costomview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;


import org.jzs.mybaseapp.R;
import org.jzs.mybaseapp.common.widget.expandtab.ViewBaseAction;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity.DataEntity.SortEntity;

import java.util.List;

/**
 * 排序
 * @author Zhanqx
 *
 */
public class PxView extends RelativeLayout implements ViewBaseAction {

	private List<SortEntity> sortList;
	
	private ListView mListView;
	private String[] items;//显示字段
	private String[] itemsId;//隐藏id
//	private final String[] items = new String[] { "全部排序", "离我最近", "人气最高", "评价最高"};//显示字段
//	private final String[] itemsVaule = new String[] { "0", "1", "2", "3" };//隐藏id
	private OnSelectListener mOnSelectListener;
	private TextAdapter adapter;
	private String mDistance;
	private String sortName = "item1";
	private Context mContext;

	public String getShowText() {
		return sortName;
	}

	public PxView(Context context) {
		super(context);
		init(context);
	}

	public PxView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public PxView(Context context, List<SortEntity> list) {
		super(context);
		sortList = list;
		init(context);
	}

	private void init(Context context) {
		mContext = context;
		items = new String[sortList.size()+1];
		itemsId = new String[sortList.size()+1];
		items[0] = "智能排序";
		itemsId[0] = "";
		for (int i = 0; i < sortList.size(); i++) {
			items[i+1] = sortList.get(i).sortName;
			itemsId[i+1] = sortList.get(i).sortId;
		}
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View inflate = inflater.inflate(R.layout.view_distance, this, true);
		inflate.setBackgroundColor(mContext.getResources().getColor(R.color.white));
//		setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_right));
		mListView = (ListView) findViewById(R.id.listView);
		adapter = new TextAdapter(context, items, R.drawable.choose_item_right, R.drawable.choose_eara_item_selector);
		adapter.setTextSize(14);
		adapter.setSelectedPositionNoNotify(0);
		if (mDistance != null) {
			for (int i = 0; i < itemsId.length; i++) {
				if (itemsId[i].equals(mDistance)) {
					adapter.setSelectedPositionNoNotify(i);
					sortName = items[i];
					break;
				}
			}
		}
		mListView.setAdapter(adapter);
		adapter.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

			@Override
			public void onItemClick(View view, int position) {

				if (mOnSelectListener != null) {
					sortName = items[position];
					mOnSelectListener.getValue(itemsId[position], items[position], position);
				}
			}
		});
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		public void getValue(String sortId, String sortName, int position);
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void show() {
		
	}

}
